#include<iostream>
#include<string>
#include<map>
#include<fstream>
#include<sstream>
#include<utility>
#include<vector>
using std::make_pair;
using std::endl;
using std::cout;
using std::map;
using std::string;
using std::ifstream;
using std::ofstream;
using std::istringstream;
using std::vector;


class ReadMap{

public:
    int readFile(const string &filename);
    void makeRule(const string &line);
    void print();
    void transFile(const string &filename2);
    void makeTrans(const string &line);
    void print2(){
        for(auto &ele : transInfo){
            cout<<ele<<endl;
        }
    }
private:
    map<string,string> _map_rule;
    vector<string> transInfo;
};

int ReadMap::readFile(const string &filename){
    string Line;
    ifstream ifs(filename);
    while(std::getline(ifs,Line)){
        makeRule(Line);
    }
    for(auto &ele : _map_rule){
        cout<<ele.first<<" "<<ele.second<<endl;
    }
}
void ReadMap::makeRule(const string &line){
    string key;
    string value;
    istringstream iss(line);
    while(iss >> key && getline(iss,value)){
        //读的第一个单词，作为key
        _map_rule.insert(make_pair(key,value));
    }
}
void ReadMap::print(){
    for(auto &ele : _map_rule){
        cout<<ele.first<<" "<<ele.second<<endl;
    }
}

void ReadMap::transFile(const string& filename2){
    string Line;
    ifstream ifs(filename2);
    string Line2;
    while(std::getline(ifs,Line)){
        makeTrans(Line);
    }
}
void ReadMap::makeTrans(const string &line){
    string world;
    string vecLine;
    istringstream iss(line);
    while(iss >> world){
        auto it = _map_rule.find(world);
        if(it !=_map_rule.end()){
            world = it->second;
        }
        if(!vecLine.empty()){
            vecLine.append(" ");
        }
        vecLine.append(world);
    }
    //移除第一个空格
    if(!vecLine.empty()&&vecLine[0]==' '){
        vecLine = vecLine.substr(1);
    }
    transInfo.push_back(vecLine);
}
int test0(){
    ReadMap test;
    test.readFile("map.txt");
    test.print();
    test.transFile("file.txt");
    test.print2();
    
}

int main(void){
    test0();
}